package com.naszetatry.registration;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.naszetatry.beans.User;
import com.naszetatry.controller.RegistrationController;
import com.naszetatry.exception.AlreadyActivatedException;
import com.naszetatry.exception.InvalidActivationKeyException;
import com.naszetatry.exception.InvalidLoginException;
import com.naszetatry.logic.UserLogic;
/**
 * Serwlet odpowiedzialny za aktywacje konta.
 * @author Maciej Koch
 *
 */
public class Activation extends HttpServlet{

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String login = request.getParameter("login");
		String activationKey = request.getParameter("activationKey");
		
		User user = new User();
		user.setLogin(login);
		user.setActivationKey(activationKey);

		boolean activationAccepted = true;
		try {
			UserLogic.activate(user);
		} catch (InvalidLoginException e) {
			activationAccepted = false;
		} catch (InvalidActivationKeyException e) {
			activationAccepted = false;
		} catch (AlreadyActivatedException e) {
			activationAccepted = false;
		}
		String path;
		if(activationAccepted) {
			path = "/jsp/activation.jsp";
			RegistrationController.setRegisteredUser(request, user);
		}
		else
			path = "/jsp/activationFailed.jsp";
					
		getServletContext().getRequestDispatcher(path).forward(request, response);
	}
	
}
